package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Customer;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.MainView;
import group2assignone.view.UITool;
import group2assignone.view.customer.CreateAccountView;
import javax.swing.JOptionPane;

/**
 * Use Case: Create Customer Account
 * Pre-Condition: User inputs details and selects 'Create Account' 
 * from the create account view
 * Post-Condition: UI is updated
 * 1. System prompts user for confirmation to create the account
 * 2. System uploads the new customer details from the view
 * 3. System checks the fields were inputted correctly
 * 4. System creates the account
 * 5. System notifies the user the account has been created (with a message)
 * 6. System updates UI to the main menu
 * 4a. - System cannot verify details (incorrect data input)
 *     - System displays error message and prompts user to input correct data
 * 5a. - System cannot create account (connection error)
 *     - System displays error message which prompts user to try again later
 * @author group2
 */
public class UCCreateCustomerAccount implements UCController {

    private Customer customer;
    private boolean ok,  okDAO;
    private int option;
    private Authentication auth;
    private CreateAccountView view;
    private MyDAO coms;
    private MainView view2;

    public UCCreateCustomerAccount(CreateAccountView view) {
        this.view = view;
        customer = new Customer();
        auth = new Authentication();
        coms = UCStartup.getInstance().getDAO();
        view2 = new MainView();
    }

    /**
     *Use Case Controller - allows the user to Create an Account from the main UI
     */
    public void run() {
        //1. System prompts user for confirmation to create the account
        option = view.confirmCreate();
        if (option == JOptionPane.YES_OPTION) {
            //2. System uploads the new customer details from the view
            view.upLoad(customer);
            //3. System checks the fields were inputted correctly
            ok = auth.checkCustFields(customer);
            if (ok) {
                //4. System creates the account
                okDAO = coms.sQLAddCustomer(customer);
                if (okDAO) {
                    //5. System notifies the user the account has been created (with a message)
                    view.notifyCreate();
                    //6. System updates UI to the main menu
                    view2.go();
                    view.dispose();
                } else {//5a. - System cannot create account (connection error)
                    //System displays error message and prompts user to try again later
                    UITool.displayError("Cannot connect to database at this time. Try again later.", view);
                }
            } else {//4a. - System cannot verify details (incorrect data input)
                //System displays error message which prompts user to input correct data
                UITool.displayError(auth.getErrorMsg(), view);
            }
        }
    }
}
